using System.Collections;
using Game.Framework.FState;
using UnityEngine;
using YooAsset;

namespace Game.Framework
{
    public class StateYooAssetUpdatePackageVersion : StateYooAssetBase
    {
        public override void Enter(StateData param)
        {
            base.Enter(param);
            YooAssetManager.Instance.StartCoroutine(UpdatePackageVersion());
        }
        
        public override void FixedUpdate(float deltaTime)
        {
            base.FixedUpdate(deltaTime);
        }
        
        public override void Update()
        {
            base.Update();
        }
        
        public override void Exit()
        {
            base.Exit();
        }
        
        private IEnumerator UpdatePackageVersion()
        {
            yield return new WaitForSecondsRealtime(0.5f);

            var package = YooAssets.GetPackage(AssetConfig.Instance.PackageName);
            var operation = package.RequestPackageVersionAsync();
            yield return operation;
            if (operation.Status != EOperationStatus.Succeed)
            {
                EventBus.Send(new EventUpdatePackageVersionFailed(operation.Error));
            }
            else
            {
                ChangeState<StateYooAssetUpdatePackageManifest>(new AssetData()
                {
                    PackageVersion = operation.PackageVersion
                });
                EventBus.Send<EventUpdatePackageVersionSucceed>();
            }
        }
    }
}


